// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::{io, path::PathBuf};

use anyhow::{Result};
use lock_box::LockBox;
use tauri::State;

// use anyhow::{Result};
mod lock_box;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    println!("app_dir:{}", app_dir());
    // println!("get_app_dir:{}", get_app_dir().unwrap().to_str().unwrap());
    format!("Hello, {}! You've been greeted from Rust!", name)
}

fn app_dir() -> String {
    std::env::current_exe()
        .map(|path| {
            path.parent()
                .unwrap_or(&std::path::Path::new(""))
                .to_str()
                .unwrap_or("")
                .to_string()
        })
        .unwrap_or_else(|_| "".to_string())
}
 
#[tauri::command]
fn is_account_exists(lock_box: State<'_, LockBox>) -> Result<bool, String> {
    lock_box.is_account_exists().map_err(|e| e.to_string())
}

#[tauri::command]
fn create_account(password: &str) -> Result<bool, String> {
    Ok(true)
}

#[tauri::command]
fn login(password: &str) -> bool {
    true
}

#[tauri::command]
fn init_lock_box() -> bool {
    true
}

fn main() {
    let lockbox = LockBox{};
    // lockbox.is_account_exists();
    tauri::Builder::default()
        .manage(lockbox)
        .invoke_handler(tauri::generate_handler![create_account, is_account_exists, login, init_lock_box])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
